import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class MyTcpServer 
{
	ServerSocket providerSocket;
	public MyTcpServer()
	{
		try
		{
			providerSocket = new ServerSocket(Program.TCP_PORT);
		}
		catch (IOException e) 
		{
			System.err.println("Socket Server failure - exiting");
			return;
		}
	}
	void run()
	{
		try
		{
			while(true)
			{
				Socket newConnection = providerSocket.accept();
				Clients.getInstance().allClientsSemaphore.acquire();
				Clients.getInstance().clients.add(new Client(newConnection));
				Clients.getInstance().allClientsSemaphore.release();
			}
		}
		catch(IOException | InterruptedException ioException)
		{
			ioException.printStackTrace();
		}
		finally
		{
			try
			{
				providerSocket.close();
			}
			catch(IOException ioException)
			{
				ioException.printStackTrace();
			}
		}
	}	
}